#include <stdio.h>
#include <stdlib.h>
#include <csp_spu.h>
#include <csp_debug.h>
#include "../../shared/poison_test.h"


int global_pid; 


int Run(){
	int pid = csp_pid();


	global_pid = csp_pid();

	int n __attribute__ ((aligned(16)));
	int poison = 0;

	csp_channel solo2collector = 
		csp_create_channel(ONE2ONE_CHANNEL, SOLO_CHANNEL, sizeof(int));

	csp_channel gang2collector = 
		csp_create_channel(ANY2ONE_CHANNEL, GANG_CHANNEL, sizeof(int));

	/* We should be able to read one value from the solo-writer process. */
	poison = csp_channel_read(solo2collector, &n);

	ASSERT( CHANNEL_SUCCESS == poison ); /* SUCCESS! */
	ASSERT( BIRTHDAY == n );

	n = 0;

	/* Try again: this time channel is supposed to be poisoned 
	 * by writing process. */
	poison = csp_channel_read(solo2collector, &n);

	ASSERT( CHANNEL_POISON == poison );
	ASSERT( BIRTHDAY != n );


	/* We start by poisoning the gang-channel. Expected result is that 
	 * all pending writing processes are released and terminated 
	 * because of poisoning. */

	csp_poison_channel( gang2collector );

	poison = csp_channel_read(gang2collector, &n);

	ASSERT( CHANNEL_POISON == poison );
	ASSERT( BIRTHDAY != n );


	ASSERT( pid == csp_pid() );
	ASSERT( global_pid == csp_pid() );

	printf("poison_collector_process terminating.\n");
		
	return COLLECTOR_RET_STATUS;
}
